<?php

namespace App\Nova;

use Illuminate\Database\Eloquent\Builder;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Image;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;

class Category extends Resource
{
    public static string $model = \App\Models\Category::class;

    public static $title = 'title';

    public static $search = [
        'title',
    ];

    public static function label(): string
    {
        return '分类';
    }

    public static function indexQuery(NovaRequest $request, $query): Builder
    {
        return $query->reorder('id');
    }

    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),

            Text::make('标题', 'title')->rules('required'),

            Image::make('缩略图', 'picture')->prunable()->creationRules('required'),

            Boolean::make('显示', 'show')->default(true),

            Number::make('顺序', 'index')->sortable()->min(0)->max(9999),

            new Panel('SEO', new SeoFields),

            DateTime::make('生成时间', 'created_at')->onlyOnDetail(),
            DateTime::make('更新时间', 'updated_at')->exceptOnForms(),

            BelongsToMany::make('视频', 'videos', Video::class),
        ];
    }
}
